package com.sc.nft.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.*;
import com.sc.nft.dto.TaskPlanPrizeDTO;
import com.sc.nft.entity.*;
import com.sc.nft.entity.vo.TaskPlanRecordVO;
import com.sc.nft.service.TaskDrawPrizeService;
import com.sc.nft.service.TaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.sc.nft.service.TaskPlanService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户任务进度表服务接口实现
 *
 * @author 张翔宇
 * @since 2023-03-29 11:14:52
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TaskPlanServiceImpl implements TaskPlanService {
    private final TaskPlanDao taskPlanDao;
    private final UserInfoDao userInfoDao;
    private final TaskDao taskDao;
    private final TaskDrawPrizeDao taskDrawPrizeDao;
    private final SaasUserTaskDao saasUserTaskDao;
    private final TaskService taskService;
    @Override
    public Page<TaskPlanRecordVO> pageAdmin(String userTel, String taskName, Date startTime, Date endTime, int pageNo, int pageSize) {
        Long userId = null;
        List<Long> taskIdList = Lists.newArrayList();
        if (StrUtil.isNotBlank(userTel)){
            UserInfo userInfo = userInfoDao.getOneByUserTel(userTel);
            if (ObjectUtil.isNull(userInfo)){
                return new Page<>(pageNo,pageSize);
            }
            userId = userInfo.getId();
        }
        if (StrUtil.isNotBlank(taskName)){
            List<Task> byTaskName = taskDao.getByTaskName(taskName);
            if (CollUtil.isEmpty(byTaskName)){
                return new Page<>(pageNo,pageSize);
            }
            taskIdList = byTaskName.stream().map(Task::getId).collect(Collectors.toList());
        }

        Page<TaskPlanRecordVO> taskPlanRecordPage = taskPlanDao.pageAdmin(userId, taskIdList, startTime, endTime, pageNo, pageSize);
        List<TaskPlanRecordVO> records = taskPlanRecordPage.getRecords();
        if (CollUtil.isEmpty(records)){
            return new Page<>(pageNo,pageSize);
        }
        List<Long> taskPlanIdList = records.stream().map(TaskPlanRecordVO::getId).collect(Collectors.toList());
        List<TaskPlanPrizeDTO> list = taskDrawPrizeDao.getTaskPrizeByTaskPlanIdList(taskPlanIdList);
        if (CollUtil.isEmpty(list)){
            return taskPlanRecordPage;
        }
        Map<Long, List<TaskPlanPrizeDTO>> map = list.stream().collect(Collectors.groupingBy(TaskPlanPrizeDTO::getTaskPlanId));
        //每个星球任务放入任务奖励明细
        for (TaskPlanRecordVO record : records) {
            List<String> taskEquityList = Lists.newArrayList();
            List<TaskPlanPrizeDTO> taskDrawPrizes = map.get(record.getId());
            for (TaskPlanPrizeDTO taskDrawPrize : taskDrawPrizes) {
                String equity = taskDrawPrize.getEquityName() + "*" + taskDrawPrize.getPrizeCount().setScale(0, RoundingMode.HALF_UP);
                taskEquityList.add(equity);
            }
            String taskEquity = StrUtil.join( ",",taskEquityList);
            record.setTaskEquity(taskEquity);
        }

        return taskPlanRecordPage;
    }

}